﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using BluSteamClient.Model;
using System.Windows.Controls;
using System.ComponentModel;
using System.Windows.Threading;
using System.Threading;

namespace BluSteamClient.ViewModel
{
    public class ChatViewModel : INotifyPropertyChanged
    {
        #region Fields

        //private ObservableCollection<ListBoxItem> _users;
        private ListBoxItem _selectedUser;
        private readonly UsersList _usersList;
        private List<string> _connectedUserList;
        public event PropertyChangedEventHandler PropertyChanged;


        #endregion

        #region Constructors

        public ChatViewModel()
        {
            _usersList = new UsersList();
            _connectedUserList = new List<string>();

            PacketReceiver.Instance.UpdateUserList += this.OnUserListChanged;
            PacketReceiver.Instance.UserList = _usersList;
        }

        #endregion

        #region Properties

        public List<string> ConnectedUserList
        {
            get { return _connectedUserList; }
            set
            {
                if (Dispatcher.CurrentDispatcher.Thread != Thread.CurrentThread)
                {
                    Console.WriteLine("not on same thread");
                }
                _connectedUserList = value;
                OnPropertyChanged("ConnectedUserList");
            }
        }

        public ListBoxItem SelectedUser
        {
            get { return _selectedUser; }
            set
            {
                _selectedUser = value;
                OnPropertyChanged("SelectedUser");
            }
        }

        #endregion

        #region Event Handlers

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void OnUserListChanged(object sender, EventArgs e)
        {
            this.ConnectedUserList = _usersList.ListUsersString;
        }

        #endregion
    }
}
